package com.estate.struts.actions;

import java.util.Date;
import java.util.List;

import com.estate.business.AnnouncementService;
import com.estate.utils.AnnouncementUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.AnnouncementVo;
import com.estate.vo.impl.UserInfoVo;

public class AnnouncementAction extends EstateSupport {

	private String id;
	private UserInfoVo postUser;
	private String content;
//	private Date postDate;
	private int readNum;
	private String title;
	private String state;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private AnnouncementService announcementService;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			AnnouncementVo announcement = this.getAddAnnouncement();
			if(announcement != null){
				boolean result = this.getAnnouncementService().addAnnouncement(announcement);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> announcementList = this.getAnnouncementService().getAnnouncementList(this.getSessionUser());
			this.getRequest().setAttribute("announcementList", announcementList);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allAnnouncement";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getAnnouncementService().getAnnouncementVo(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("announcement", vo);
			this.getAnnouncementService().updateReadNum((AnnouncementVo) vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getAnnouncementService().getAnnouncementVo(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("announcement", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			AnnouncementVo announcement = this.getUpdateAnnouncement();
			if(announcement == null){
				page = "fail";
			}else{
				boolean result = this.getAnnouncementService().updateAnnouncement(announcement);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}else if("post".equals(this.getMethod())){
			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
			page = this.SUCCESS;
		}else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getAnnouncementService().deleteMultiAnnouncement(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					AnnouncementVo announcement = new AnnouncementVo();
					announcement.setId(Integer.parseInt(this.getId()));
					boolean result = this.getAnnouncementService().deleteAnnouncement(announcement);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		return page;
	}
	private AnnouncementVo getUpdateAnnouncement(){
		AnnouncementVo announcement = new AnnouncementVo();
		
		int id = Integer.parseInt(this.getId());
		
		announcement.setId(id);
		announcement.setContent(this.getContent());
		announcement.setTitle(this.getTitle());
		int stateInt = Integer.parseInt(this.getState());
		announcement.setState(AnnouncementUtil.getAnnouncementString(stateInt));
		return announcement;
	}
	private AnnouncementVo getAddAnnouncement(){
		AnnouncementVo announcement = new AnnouncementVo();
		
		announcement.setContent(this.getContent());
		announcement.setTitle(this.getTitle());
		int stateInt = Integer.parseInt(this.getState());
		announcement.setState(AnnouncementUtil.getAnnouncementString(stateInt));
		if(stateInt == 2){
			announcement.setPostDate(new Date());
			announcement.setReadNum(0);
		}
		announcement.setPostUser(this.getSessionUser());
			
		return announcement;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public UserInfoVo getPostUser() {
		return postUser;
	}
	public void setPostUser(UserInfoVo postUser) {
		this.postUser = postUser;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getReadNum() {
		return readNum;
	}
	public void setReadNum(int readNum) {
		this.readNum = readNum;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}
	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}
}
